<?php
class BaseTypeElement{
	var $typeName = "";
	var $name = "";
	var $formName = "";
	var $template = "";
	var $templateGeneral = "ctype/views/base/baseTypeElement.php";
	var $properties = array();
	var $required = FALSE;
	
	var $repeat = FALSE;
	var $maxRepeat = 100;
	var $minRepeat = -1;
	
	var $ci;
	
	/*
	 * Contenido estatico asociado
	 */
	var $static = array(
		"javascript" => array(
		),
		"style" => array(
		)		
	);
	
	function BaseTypeElement(){
		$this->typeName = strtolower(get_class($this));
		$this->ci =& get_instance();
	}
	
	function setTypeName($name){
		$this->typeName = $name;
	}
	
	function getTypeName(){
		return $this->typeName;
	}
	
	function setFormName($name){
		$this->formName = $name;
	}
	
	function getFormName(){
		return $this->formName;
	}
	
	function setName($name){
		$this->name = $name;
	}
	
	function getName(){
		return $this->name;
	}
	
	function getRequired(){
		return $this->required;
	}
	
	function setRequired($required){
		$this->required = $required;
	}
	
	function addProperty($name, $value){
		$this->properties[$name] = $value;
	}
	
	function getProperty($name){
		if(isset($this->properties[$name])){
			return $this->properties[$name];
		}
		
		return FALSE;
	}
	
	function addProperties($properties){
		if($properties != NULL){
			foreach($properties as $key=>$value){
				$this->addProperty($key, $value);
			}
		}
	}
	
	function getProperties(){
		return $this->properties;
	}
	
	function addStatic($type, $path){
		//echo "Añadiendo: $path";
		if(isset($this->static[$type])){
			//@TODO Comprobar si ya existe la libreria a añadir
			if(array_search($path, $this->static[$type]) === FALSE){
				array_push($this->static[$type], $path);
			}
		}
	}
	
	function addStatics($statics){
		//echo "<br/>Añadiendo statics";
		$keys = array_keys($statics);
		//print_r($statics);
		foreach($keys as $key){
			$staticsTmp = $statics[$key];
			foreach($staticsTmp as $static){
				$this->addStatic($key, $static);
			}
		}
	}
	
	function getStatics(){
		return $this->static;
	}
	
	function setRepeat($repeat){
		$this->repeat = $repeat;
	}
	
	function getRepeat(){
		return $this->repeat;
	}
	
	function setMinRepeat($value){
		$this->minRepeat = $value;
	}
	
	function getMinRepeat(){
		return $this->minRepeat;
	}

	function setMaxRepeat($value){
		$this->maxRepeat = $value;
	}

	function getMaxRepeat(){
		return $this->maxRepeat;
	}
	
	
	function view($node = NULL, $data = NULL){
		$view = "";
		
		//@TODO El template general, es unico y los templates especificos (1 por repeat) se meteran dentro de este...
		//En el template general tendremos una funcion para ir añadiendo y ocultando por nombre_$i...
		
		if($this->getRepeat()){
			$viewField = "";
			
			$pos = 0;
			for($i=0; $i<=$this->getMaxRepeat(); $i++){
				$params = array();
				if(is_array($data) && isset($data[$i])){
					$params['value'] = $data[$i];
					$pos = $i;
				} else{
					$params['value'] = NULL;
				}
				
				$params['name'] = $this->formName . $i;
				$params['properties'] = $this->properties;
				
				if($i > 0){
					$params['properties']['title'] = $params['properties']['title'] . "(".($i+1).")";
				}
				
				$viewField .= $this->getFileContent($this->template, $params);
			}
			
			$params['name'] = $this->formName;
			$params['properties'] = $this->properties;
			$params['field'] = $viewField;
			$params['max'] = $this->getMaxRepeat();
			$params['pos'] = $pos;
			$view .= $this->getFileContent($this->templateGeneral, $params);
			
		} else{
			$params = array();
			$params['value'] = $data;
			$params['name'] = $this->formName;
			$params['properties'] = $this->properties;
			
			$viewField = $this->getFileContent($this->template, $params);
			
			$params = array();
			$params['value'] = $data;
			$params['name'] = $this->formName;
			$params['properties'] = $this->properties;
			$params['field'] = $viewField;
			$params['max'] = $this->getMaxRepeat();
			$params['pos'] = $pos;
			
			$view = $this->getFileContent($this->templateGeneral, $params);
		}
		

		return $view;
	}
	
	
	function getFileContent($path, $data){
		extract($data);
		
		ob_start();
		eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($path, TRUE))));
		$view = ob_get_contents();
		@ob_end_clean();
		
		return $view;
	}
	
	function checkCorrectSave(){
		if($this->getRequired()){
			$this->ci->form_validation->set_rules($this->formName, $this->properties['title'], 'required');
		} else{
			//Trick para poder hacer el set_value
			$this->ci->form_validation->set_rules($this->formName, "", "");
		}
		
		if(!$this->ci->form_validation->run()){
			return FALSE;
		}
		
		return TRUE;
	}
	
	function save($node = NULL, $data = NULL){
		/*if($this->getRequired()){
			$this->ci->form_validation->set_rules($this->formName, $this->properties['title'], 'required');
		}
		*/
		if($this->ci->form_validation->run()){
			$value = $this->ci->input->post($this->formName);
			if($value != NULL){
				//$data[$this->name] = $value;
				return $value;
			}
		} else{
			//echo "CAMPO " . $this->name . " REQUERIDO<br/>";
			//@TODO volver al formulario hasta que este correcto...
		}
		
		return NULL;
	}
}
?>